package cn.com.jtang.wedding.action;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import cn.com.jtang.wedding.model.AdminUser;

import cn.com.jtang.wedding.model.User;
import cn.com.jtang.wedding.service.UserManager;
import cn.com.jtang.wedding.util.EncryptUtil;
import cn.com.jtang.wedding.util.NumberUtil;

import com.opensymphony.xwork2.Preparable;

public class UserAction extends BaseAction implements Preparable {

	private UserManager userManager;

	private User user;
	
	private String userName;    	//必填		用户名
	private String password;    	//必填		密码
	private String husband;  		//必填		新郎名
    private String wife;  			//必填		新娘名
    private String mobile;     		//必填		联系电话
    private String email="";					// 	联系email
    private String notes="";					//备注
    private Date registTime;		//必填		注册时间
    private String province;    	//必填		省
    private String city;   			//必填		市
    
    private String registertime;
    private String resetPassword;
    
    private long uid;
	private String id;
	private String[] chb;
	
	private List<User> users;
	Map<String, Object> map = new HashMap<String, Object>();

	private String userAccount;
	private String resetPass;

	private Long pid;
	private String passwordAgain;
	//private String password;

	public void prepare() throws Exception {

		// TODO Auto-generated method stub
	}

	/**
	 * 方法描述：用户首页
	 * 
	 * @return
	 */
	public String top() {
		user = getCurrentUser();
		return SUCCESS;
	}

	/**
	 * 方法描述：读取当前用户信息
	 */
	public String userInfo() {

		user = userManager.queryById(this.getCurrentUser().getId());
		return "success";
	}

	public String doUserLogin() {

		if (userAccount == null || password == null) {
			return ERROR;
		}
		User user = userManager.queryByUsername(userAccount);
		if (user == null) {
			return ERROR;
		}
		if (user.getPassword().equals(EncryptUtil.Encrypt(password))) {
			HttpSession session = getSession();
			session.putValue("currentuser", user);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public String doUserModify() {
		User cUser = this.getCurrentUser();
		User nUser = this.userManager.queryById(cUser.getId());
		
		nUser.setHusband(this.getHusband());
		nUser.setWife(this.getWife());
		nUser.setMobile(this.getMobile());
		nUser.setEmail(this.getEmail());
		nUser.setNotes(this.getNotes());
		nUser.setProvince(this.getProvince());
		nUser.setCity(this.getCity());
		userManager.updateById(cUser.getId(), nUser);
		user = userManager.queryById(cUser.getId());
		return SUCCESS;
	}
	
	public String getUserAccount() {

		return userAccount;
	}

	public void setUserAccount(String userAccount) {

		this.userAccount = userAccount;
	}


	/**
	 * @return the user
	 */
	public User getUser() {

		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {

		this.user = user;
	}

	public UserManager getUserManager() {

		return userManager;
	}
	
	public String insert(){
		if(userName!=null&&userManager.queryByUsername(userName)!=null){
			return "userExist";
		}
		if(password==null||password.equals("")||(!password.equals(password))){
			return "passwordError";
		}
		if(userName!=null&&husband!=null&&wife!=null&&mobile!=null
				&&province!=null&&city!=null)	{
			user = new User();
			password=EncryptUtil.Encrypt(password);
			user.setUserName(userName);
			user.setPassword(password);
			user.setHusband(husband);
			user.setWife(wife);
			user.setMobile(mobile);
			user.setProvince(province);
			user.setCity(city);
			user.setEmail(email);
			user.setNotes(notes);
			user.setRegistTime(new Date());
			user.setParentId(getCurrentAdminUser().getId());
			
			userManager.add(user);			
			return SUCCESS;
		}else {
			return ERROR;
		}
		
	}
	/**
	 * 多条记录删除
	 */
	public String delete(){
		user = new User();
		if(chb!=null)
		{
			for(int i=0;i<chb.length;i++){
				uid = new Long(chb[i]).longValue();
				userManager.deleteById(uid);
			}
		}
		return SUCCESS;
	}
	/**
	 * 
	 * 单条记录删除
	 */
	public String deleteforSingle(){
		user = new User();
		if(id!=null){
			uid = new Long(id).longValue();
			userManager.deleteById(uid);
		}
		return SUCCESS;		
	}
	
	public String update(){
		User u=userManager.queryByUsername(userName);
		if(u!=null&&(!u.getId().equals(Long.valueOf(id)))){
			return "userExist";
		}
		user = new User();
		uid = new Long(id).longValue();
		user = userManager.queryById(uid);
		user.setId(uid);
		user.setUserName(userName);
		//user.setPassword(password);
		user.setHusband(husband);
		user.setWife(wife);
		user.setMobile(mobile);
		user.setProvince(province);
		user.setCity(city);
		user.setEmail(email);
		user.setNotes(notes);
		user.setRegistTime(userManager.queryById(uid).getRegistTime());
		user.setParentId(getCurrentAdminUser().getId());
		userManager.updateById(uid, user);
		return SUCCESS;
	}
	/**
	 * 获取当前选中记录信息
	 */
	public String getUserforUpdate(){
		user = new User();
		uid = new Long(id).longValue();
		user = userManager.queryById(uid);
		userName = user.getUserName();
		password = user.getPassword();
		husband = user.getHusband();
		wife = user.getWife();
		mobile = user.getMobile();
		province = user.getProvince();
		city = user.getCity();
		email = user.getEmail();
		notes = user.getNotes();
		registertime = user.getRegistTime().toString();
		return SUCCESS;	
	}
	
	/**
	 * 获取所有记录列表
	 */
	public String getAll(){		
		pageNum = (pageNum) <= 0 ? 1 : pageNum;
		map.put("parentId", getCurrentAdminUser().getId());
		int count = userManager.getCount(map);
		//int count = userManager.getCount(null);
		users = new ArrayList<User>();
		pageCount = (int) Math.ceil((double) count / (double) pageSize);
		pageCount=pageCount==0?1:pageCount;
		pageNum = pageNum > pageCount ? pageCount : pageNum;
		users = userManager.queryAll(map, pageSize * (pageNum - 1),
				pageSize * pageNum);
		return SUCCESS;
	}

	/**
	 * 密码找回
	 */
	public String resetPassword(){
		/*resetPassword=NumberUtil.getUniqueID().substring(0, 6);
		User user = null;
		user=userManager.queryById(getCurrentUser().getId());
		user.setPassword(EncryptUtil.Encrypt(resetPassword));
		userManager.updateById(getCurrentUser().getId(), user);		
		//System.out.println(resetPassword);
		resetPass=resetPassword;
		return SUCCESS;*/
		if(pid==null||pid.equals(Long.valueOf(0))){
			return "repetition";
		}
		
		resetPass=NumberUtil.getUniqueID().substring(0, 6);
		user=userManager.queryById(pid);
		user.setPassword(EncryptUtil.Encrypt(resetPass));
		userManager.updateById(pid, user);
		return SUCCESS;
	}
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getHusband() {
		return husband;
	}

	public void setHusband(String husband) {
		this.husband = husband;
	}

	public String getWife() {
		return wife;
	}

	public void setWife(String wife) {
		this.wife = wife;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public Date getRegistTime() {
		return registTime;
	}

	public void setRegistTime(Date registTime) {
		this.registTime = registTime;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getRegistertime() {
		return registertime;
	}

	public void setRegistertime(String registertime) {
		this.registertime = registertime;
	}

	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String[] getChb() {
		return chb;
	}

	public void setChb(String[] chb) {
		this.chb = chb;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	public void setUserManager(UserManager userManager) {

		this.userManager = userManager;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public String getResetPassword() {
		return resetPassword;
	}

	public void setResetPassword(String resetPassword) {
		this.resetPassword = resetPassword;
	}

	public String getResetPass() {
		return resetPass;
	}

	public void setResetPass(String resetPass) {
		this.resetPass = resetPass;
	}

	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}

	public String getPasswordAgain() {
		return passwordAgain;
	}

	public void setPasswordAgain(String passwordAgain) {
		this.passwordAgain = passwordAgain;
	}
	
}
